using System.Collections.Generic;
using Kirurobo;
using UnityEngine;
using UnityEngine.UI;

public class UIMainPanel : BaseView
{
    protected override bool RecordPos => true;
    private Button _btnClose;
    private Slider _sliderSpeed;
    private Slider _sliderSize;
    private Text _textSpeed;
    private Text _textSize;
    private Button _btnQuit;
    private Toggle _tglNeverSleep;
    private Toggle _tglShowFloatKeys;
    private Button _btnLog;
    private Toggle _tglAutoStart;
    private Button _btnSwitchMonitor;
    private Dropdown _dropAudio;
    private Slider _sliderAlpha;
    private Text _textAlpha;
    private Button _btnRecord;
    protected override void OnInit()
    {
        _btnClose = gameObject.transform.FindChildEx("ButtonClose").GetComponent<Button>();
        _sliderSpeed = gameObject.transform.FindChildEx("Speed/Slider").GetComponent<Slider>();
        _sliderSize = gameObject.transform.FindChildEx("Size/Slider").GetComponent<Slider>();
        _btnQuit = gameObject.transform.FindChildEx("ButtonQuit").GetComponent<Button>();
        _textSpeed = gameObject.transform.FindChildEx("Speed/TextValue").GetComponent<Text>();
        _textSize = gameObject.transform.FindChildEx("Size/TextValue").GetComponent<Text>();
        _tglNeverSleep = gameObject.transform.FindChildEx("NeverSleep/Toggle").GetComponent<Toggle>();
        _tglShowFloatKeys = gameObject.transform.FindChildEx("ShowFloatKeys/Toggle").GetComponent<Toggle>();
        _btnLog = gameObject.transform.FindChildEx("ButtonLog").GetComponent<Button>();
        _tglAutoStart = gameObject.transform.FindChildEx("AutoStart/Toggle").GetComponent<Toggle>();
        _btnSwitchMonitor = gameObject.transform.FindChildEx("ButtonSwitchMonitor").GetComponent<Button>();
        _dropAudio = gameObject.transform.FindChildEx("Audio/Dropdown").GetComponent<Dropdown>();
        _sliderAlpha = gameObject.transform.FindChildEx("Alpha/Slider").GetComponent<Slider>();
        _textAlpha = gameObject.transform.FindChildEx("Alpha/TextValue").GetComponent<Text>();
        _btnRecord = gameObject.transform.FindChildEx("ButtonRecord").GetComponent<Button>();

        _btnClose.onClick.AddListener(() => CloseSelf());
        _sliderSpeed.onValueChanged.AddListener((value) => {
            GameSetting.Instance.settingData.speed = value;
            _textSpeed.text = value.ToString("F1");
            EventMgr.Instance.SendEvent(EventType.SpeedChanged);
        });
        _sliderSize.onValueChanged.AddListener((value) => {
            GameSetting.Instance.settingData.size = value;
            _textSize.text = value.ToString("F1");
            EventMgr.Instance.SendEvent(EventType.ScaleChanged);
        });
        _btnQuit.onClick.AddListener(() => {
            GameManager.Instance.QuitGame();
        });
        _tglNeverSleep.onValueChanged.AddListener((value) => {
            GameSetting.Instance.settingData.neverSleep = value;
        });
        _tglShowFloatKeys.onValueChanged.AddListener((value) => {
            GameSetting.Instance.settingData.showFloatKeys = value;
        });
        _btnLog.onClick.AddListener(() => {
            Reporter.Instance.doShow();
        });
        _btnRecord.onClick.AddListener(() => {
            UIMgr.Instance.Open<UIKeyRecordPanel>();
        });
        _tglAutoStart.onValueChanged.AddListener((value) => {
            AutoStartMgr.Instance.SetAutoStart(value);
        });
        _btnSwitchMonitor.onClick.AddListener(() => {
            UniWindowController.current.monitorToFit = (UniWindowController.current.monitorToFit + 1) % UniWindowController.GetMonitorCount();
            EventMgr.Instance.SendEvent(EventType.SwitchMonotor);
        });
        _dropAudio.onValueChanged.AddListener((value) => {
            if (value >= (int)CV.Count)
            {
                value = (int)CV.Random;
            }
            GameSetting.Instance.settingData.cv = (CV)value;
        });
        _sliderAlpha.onValueChanged.AddListener((value) => {
            GameSetting.Instance.settingData.alpha = value;
            _textAlpha.text = value.ToString("F2");
            EventMgr.Instance.SendEvent(EventType.AlphaChanged);
        });

        _sliderSpeed.value = GameSetting.Instance.settingData.speed;
        _sliderSize.value = GameSetting.Instance.settingData.size;
        _textSpeed.text = GameSetting.Instance.settingData.speed.ToString("F1");
        _textSize.text = GameSetting.Instance.settingData.size.ToString("F1");
        _tglNeverSleep.isOn = GameSetting.Instance.settingData.neverSleep;
        _tglShowFloatKeys.isOn = GameSetting.Instance.settingData.showFloatKeys;
        _tglAutoStart.isOn = AutoStartMgr.Instance.IsAutoStartEnabled();
        _btnSwitchMonitor.transform.parent.gameObject.SetActive(UniWindowController.GetMonitorCount() > 1);
        var opts = new List<Dropdown.OptionData>();
        foreach (var item in GameManager.cvMap)
        {
            opts.Add(new Dropdown.OptionData(item.Key.ToString()));
        }
        opts.Add(new Dropdown.OptionData("???"));
        _dropAudio.options = opts;
        _dropAudio.value = (int)GameSetting.Instance.settingData.cv > (int)CV.Count ? (int)CV.Count : (int)GameSetting.Instance.settingData.cv;
        _sliderAlpha.value = GameSetting.Instance.settingData.alpha;
        _textAlpha.text = GameSetting.Instance.settingData.alpha.ToString("F2");
    }

    protected override void OnDispose()
    {
        GameSetting.Instance.Save();
    }
}